package facade;

import controller.Controller;
import model.AgriculturalMachine;
import factory.AgriculturalMachineFactory;
import factory.HarvesterFactory;
import factory.TractorFactory;
import sensor.ConcreteSensor;
import sensor.Sensor;
import handler.Handler;
import handler.WarningHandler;
import command.StartCommand;
import command.StopCommand;
import strategy.DryLandStrategy;
import strategy.WetLandStrategy;

public class AgriculturalSystemFacade {
    private AgriculturalMachine tractor;
    private AgriculturalMachine harvester;
    private Controller controller;
    private Sensor sensor;
    private Handler warningHandler;

    public AgriculturalSystemFacade() {
        // 使用工厂创建农机
        AgriculturalMachineFactory tractorFactory = new TractorFactory();
        AgriculturalMachineFactory harvesterFactory = new HarvesterFactory();
        tractor = tractorFactory.createMachine();
        harvester = harvesterFactory.createMachine();

        // 设置路径规划策略
        tractor.setPathPlanningStrategy(new DryLandStrategy());
        harvester.setPathPlanningStrategy(new WetLandStrategy());

        // 初始化命令控制器
        controller = new Controller();

        // 初始化传感器
        sensor = new ConcreteSensor();
        sensor.registerObserver(data -> System.out.println("传感器数据: " + data));

        // 初始化责任链
        warningHandler = new WarningHandler();
        Handler errorHandler = new handler.ErrorHandler();
        Handler criticalHandler = new handler.CriticalHandler();

        warningHandler.setSuccessor(errorHandler);
        errorHandler.setSuccessor(criticalHandler);
    }

    public void startTractor() {
        controller.setCommand(new StartCommand(tractor));
        controller.executeCommand();
        System.out.println("拖拉机已启动");
    }

    public void stopTractor() {
        controller.setCommand(new StopCommand(tractor));
        controller.executeCommand();
        System.out.println("拖拉机已停止");
    }

    public void startHarvester() {
        controller.setCommand(new StartCommand(harvester));
        controller.executeCommand();
        System.out.println("收割机已启动");
    }

    public void stopHarvester() {
        controller.setCommand(new StopCommand(harvester));
        controller.executeCommand();
        System.out.println("收割机已停止");
    }

    public String tractorPlanPath() {
        String path = tractor.planPath();
        System.out.println("拖拉机路径规划: " + path);
        return path;
    }

    public String harvesterPlanPath() {
        String path = harvester.planPath();
        System.out.println("收割机路径规划: " + path);
        return path;
    }

    public void simulateSensorData(String data) {
        sensor.setSensorData(data);
    }

    public void handleSystemEvent(String event) {
        System.out.println("处理系统事件: " + event);
        warningHandler.handleRequest(event);
    }

    public String getTractorStatus() {
        return tractor.getStatus();
    }

    public String getHarvesterStatus() {
        return harvester.getStatus();
    }
}